package com.xyyl.patient.common.dypls;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dyplsapi.model.v20170525.*;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.xyyl.patient.core.util.DateUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author kellie
 * @ClassName: AliyunDyplsUtil
 * @Description:阿里云号码隐私保护
 * @date 2017年5月24日
 */
@Component
public class AliyunDyplsUtil {

    private static Logger logger = LogManager.getLogger(AliyunDyplsUtil.class);

    /**
     * product 隐私号码产品名称（隐私保护产品名固定，无需修改）
     */
    private static String product = "Dyplsapi";
    /**
     * 隐私号码产品域名（接口地址固定，无需修改）
     */
    private static String domain = "dyplsapi.aliyuncs.com";

    /**
     * 阿里云号池Key
     */
    private static String poolKey = "FC100000034068025";
    /**
     * 绑定关系对应的失效时间-不能早于当前系统时间  设置半小时后过期
     */
    private static String Expiration = DateUtils.addHour(new Date(System.currentTimeMillis()), 6000);

    /**
     * 阿里云 AccessKeyId
     */
    private static String aliyunAccessKeyId;

    @Value("#{aliyun['oss.accessKeyId']}")
    public void setAliAccessKeyId(String accessKeyId) {
        aliyunAccessKeyId = accessKeyId;
    }

    /**
     * 阿里云 AccessKeySecret
     */
    private static String aliyunAccessKeySecret;

    @Value("#{aliyun['oss.accessKeySecret']}")
    public void setAliyunAccessKeySecret(String accessKeySecret) {
        aliyunAccessKeySecret = accessKeySecret;
    }


    /**
     * AXB绑定示例
     *
     * @return
     * @throws ClientException
     */
    public static BindAxbResponse bindAxb(String Aphone, String Bphone) throws ClientException {
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunAccessKeyId, aliyunAccessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        BindAxbRequest request = new BindAxbRequest();
        //必填:对应的号池Key
        request.setPoolKey(poolKey);
        //必填:AXB关系中的A号码
        request.setPhoneNoA(Aphone);
        //必填:AXB关系中的B号码
        request.setPhoneNoB(Bphone);
        //必填:绑定关系对应的失效时间-不能早于当前系统时间
        request.setExpiration(Expiration);
        //可选:是否需要录制音频-默认是false
        request.setIsRecordingEnabled(false);

        //外部业务自定义ID属性
        request.setOutId("yourOutId");

        //hint 此处可能会抛出异常，注意catch
        BindAxbResponse response = null;
        try {
            response = acsClient.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 订购关系解绑示例(解绑接口不区分AXB、AXN)
     *
     * @return
     * @throws ClientException
     */
    public static UnbindSubscriptionResponse unbind(String subsId, String secretNo) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunAccessKeyId, aliyunAccessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象
        UnbindSubscriptionRequest request = new UnbindSubscriptionRequest();
        //必填:对应的号池Key
        request.setPoolKey(poolKey);
        //必填-分配的X小号-对应到绑定接口中返回的secretNo;
        request.setSecretNo(secretNo);
        //可选-绑定关系对应的ID-对应到绑定接口中返回的subsId;
        request.setSubsId(subsId);

        UnbindSubscriptionResponse response = acsClient.getAcsResponse(request);

        return response;
    }

    //更新绑定关系  xPhone 中间号, bPhone 患者号
    public static UpdateSubscriptionResponse updateSubscription(Integer subsId, String xPhone, String bPhone) throws ClientException {

        //设置超时时间-可自行调整
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化ascClient,暂时不支持多region
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunAccessKeyId,
                aliyunAccessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象-具体描述见控制台-文档部分内容
        UpdateSubscriptionRequest request = new UpdateSubscriptionRequest();
        //绑定关系对应的号池Key
        request.setPoolKey(poolKey);
        //必填:绑定关系ID
        request.setSubsId(subsId.toString());
        //必填:绑定关系对应的X号码
        request.setPhoneNoX(xPhone);
        //必填:操作类型指令支持,支持的操作类型详见文档
        request.setOperateType("updateNoB");
        //可选:需要修改的B号码
        request.setPhoneNoB(bPhone);
        //hint 此处可能会抛出异常，注意catch
        UpdateSubscriptionResponse response = null;
        try {
            response = acsClient.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }
}
